//
//  JSUITool.m
//  JSKit
//
//  Created by Apple on 2019/7/22.
//  Copyright © 2019 Apple. All rights reserved.
//

#import "JSUITool.h"
#import <QMUIKit/QMUIKit.h>
#import "JSBusiness.h"


@implementation JSUITool

- (instancetype)init{
    if (self = [super init]) {
        _screenWidth = [UIScreen mainScreen].bounds.size.width;
        _screenHeight = [UIScreen mainScreen].bounds.size.height;
    
    }
    return self;
}
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static JSUITool *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
        instance.qmui_multipleDelegatesEnabled = YES;
        [instance setFit:^CGFloat(CGFloat oriValue) {
            
            return oriValue * instance.screenWidth/375;
        }];
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [self sharedInstance];
}
- (CGFloat)bodyFontSize{
   
    if (_bodyFontSize <= 0) {
        _bodyFontSize = 15;
    }
    return (self.fit&&self.neeFitFont)?self.fit(_bodyFontSize): _bodyFontSize;
}
- (CGFloat)mediumFontSize{
    if (_mediumFontSize <= 0) {
        _mediumFontSize = 12;
    }
    return (self.fit&&self.neeFitFont)?self.fit(_mediumFontSize): _mediumFontSize;;
}
- (CGFloat)lowerFontSize{
    if (_lowerFontSize <= 0) {
        _lowerFontSize = 10;
    }
    return (self.fit&&self.neeFitFont)?self.fit(_lowerFontSize): _lowerFontSize;;
}
- (NSInteger)bodyBlack{
    if (_bodyBlack <= 0) {
        return 0x1b1a20;
    }
    return _bodyBlack;
}
- (NSInteger)mediumBlack{
    if (_mediumBlack <= 0) {
        return 0x333333;
    }
    return _mediumBlack;
}
- (NSInteger)lowerBlack{
    if (_lowerBlack <= 0) {
        return 0x666666;
    }
    return _lowerBlack;
}
- (NSInteger)bodyGray{
    if (_bodyGray <= 0) {
        return 0x666666;
    }
    return _bodyGray;
}
- (NSInteger)mediumGray{
    if (_mediumGray <= 0) {
        return 0x999999;
    }
    return _mediumGray;
}
- (NSInteger)lowerGray{
    if (_lowerGray <= 0) {
        return 0xaaaaaa;
    }
    return _lowerGray;
}

@end

@implementation UILabel (JSUITool)

- (UILabel * _Nonnull (^)(NSString * _Nonnull))js_text{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(NSString *str){
        weakSelf.text = str;
        return weakSelf;
    };
}
- (UILabel * _Nonnull (^)(UIFont * _Nonnull))js_textFont{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(UIFont *font){
        weakSelf.font = font;
        return weakSelf;
    };
    
}
- (UILabel * _Nonnull (^)(UIColor * _Nonnull))js_textColor{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(UIColor *color){
        weakSelf.textColor = color;
        return weakSelf;
    };
}
- (UILabel * _Nonnull (^)(NSLineBreakMode))js_lineBreakMode{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(NSLineBreakMode mode){
        weakSelf.lineBreakMode = mode;
        return weakSelf;
    };
}

- (UILabel * _Nonnull (^)(NSTextAlignment))js_textAlignment{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(NSTextAlignment alignment){
        weakSelf.textAlignment = alignment;
        return weakSelf;
    };
}

-(UILabel * _Nonnull (^)(NSAttributedString * _Nonnull))js_attributedString{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(NSAttributedString *attrString){
        weakSelf.attributedText = attrString;
        return weakSelf;
    };
}

-(UILabel * _Nonnull (^)(NSInteger))js_lableNumbOfLines{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(NSInteger num){
        weakSelf.numberOfLines = num;
        return weakSelf;
    };
}
- (UILabel * _Nonnull (^)(UIBaselineAdjustment))js_baselineAdjustment{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(UIBaselineAdjustment num){
        weakSelf.baselineAdjustment = num;
        return weakSelf;
    };
}
- (UILabel * _Nonnull (^)(UIColor * _Nonnull))js_highlightedTextColor{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(UIColor *color){
        weakSelf.highlightedTextColor = color;
        return weakSelf;
    };
}
- (UILabel * _Nonnull (^)(NSString * _Nonnull))js_debugText{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(NSString *text){
        if ([JSBusiness sharedInstance].isDebug) {
            weakSelf.text = text;
        }
        return weakSelf;
    };
}
- (UILabel * _Nonnull (^)(NSString * _Nonnull, UIFont * _Nonnull, UIColor * _Nonnull, NSString * _Nonnull))js_createLabel{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(NSString *text, UIFont *font, UIColor *color, NSString *debugText){
        weakSelf.js_textFont(font).js_textColor(color).js_debugText(debugText);
        if (text.length) {
            weakSelf.text = text;
        }
        return weakSelf;
    };
}
- (UILabel * _Nonnull (^)(NSString * _Nonnull, UIFont * _Nonnull, UIColor * _Nonnull, NSLineBreakMode, NSInteger, NSString * _Nonnull))js_createBreakLabel{
    __weak typeof(self) weakSelf = self;
    return ^UILabel*(NSString *text, UIFont *font, UIColor *color, NSLineBreakMode breakMode, NSInteger numberOfLines, NSString *debugText){
        weakSelf.js_createLabel(text, font, color, debugText).js_lineBreakMode(breakMode).js_lableNumbOfLines(numberOfLines);
        
        return weakSelf;
    };
}
@end

@implementation YYLabel (JSUITool)

- (YYLabel * _Nonnull (^)(NSString * _Nonnull))js_text{
    __weak typeof(self) weakSelf = self;
    return ^YYLabel*(NSString *str){
        weakSelf.text = str;
        return weakSelf;
    };
}
- (YYLabel * _Nonnull (^)(UIFont * _Nonnull))js_textFont{
    __weak typeof(self) weakSelf = self;
    return ^YYLabel*(UIFont *font){
        weakSelf.font = font;
        return weakSelf;
    };
    
}
- (YYLabel * _Nonnull (^)(UIColor * _Nonnull))js_textColor{
    __weak typeof(self) weakSelf = self;
    return ^YYLabel*(UIColor *color){
        weakSelf.textColor = color;
        return weakSelf;
    };
}
- (YYLabel * _Nonnull (^)(NSLineBreakMode))js_lineBreakMode{
    __weak typeof(self) weakSelf = self;
    return ^YYLabel*(NSLineBreakMode mode){
        weakSelf.lineBreakMode = mode;
        return weakSelf;
    };
}

- (YYLabel * _Nonnull (^)(NSTextAlignment))js_textAlignment{
    __weak typeof(self) weakSelf = self;
    return ^YYLabel*(NSTextAlignment alignment){
        weakSelf.textAlignment = alignment;
        return weakSelf;
    };
}

-(YYLabel * _Nonnull (^)(NSAttributedString * _Nonnull))js_attributedString{
    __weak typeof(self) weakSelf = self;
    return ^YYLabel*(NSAttributedString *attrString){
        weakSelf.attributedText = attrString;
        return weakSelf;
    };
}

-(YYLabel * _Nonnull (^)(NSInteger))js_lableNumbOfLines{
    __weak typeof(self) weakSelf = self;
    return ^YYLabel*(NSInteger num){
        weakSelf.numberOfLines = num;
        return weakSelf;
    };
}

- (YYLabel * _Nonnull (^)(NSString * _Nonnull))js_debugText{
    __weak typeof(self) weakSelf = self;
    return ^YYLabel*(NSString *text){
        if (isDebug) {
            weakSelf.text = text;
        }
        return weakSelf;
    };
}


- (YYLabel * _Nonnull (^)(NSString * _Nonnull, UIFont * _Nonnull, UIColor * _Nonnull, NSString * _Nonnull))js_createLabel{
    __weak typeof(self) weakSelf = self;
    return ^YYLabel*(NSString *text, UIFont *font, UIColor *color, NSString *debugText){
        weakSelf.js_textFont(font).js_textColor(color).js_debugText(debugText);
        if (text.length) {
            weakSelf.text = text;
        }
        return weakSelf;
    };
}
@end

@implementation UIView (JSUITool)

- (UIView * _Nonnull (^)(UIColor * _Nonnull))js_bgColor{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(UIColor *color){
        weakSelf.backgroundColor = color;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(BOOL))js_maskToBouns{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(BOOL clip){
        weakSelf.layer.masksToBounds = clip;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGSize, CGFloat, CGFloat, UIColor *))js_shadow{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGSize offset, CGFloat opacity, CGFloat radius, UIColor *color){
        weakSelf.layer.shadowOffset = offset;
        weakSelf.layer.shadowOpacity = opacity;
        if (radius) {
             weakSelf.layer.shadowRadius = radius;
        }
        weakSelf.layer.shadowColor = color?color.CGColor:[UIColor lightGrayColor].CGColor;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(UIViewTintAdjustmentMode))js_tintAdjustmentMode{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(UIViewTintAdjustmentMode mode){
        weakSelf.tintAdjustmentMode = mode;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGFloat, CGSize, CGFloat, CGFloat))js_cornerRadiusShadow{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGFloat cornerRadius, CGSize offset, CGFloat opacity, CGFloat radius){
        return weakSelf.js_radius(cornerRadius).js_shadow(offset, opacity, radius,[UIColor lightGrayColor]);
    };
}
- (UIView * _Nonnull (^)(UIColor * _Nonnull, CGFloat, CGFloat))js_border{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(UIColor *borderColor, CGFloat borderWidth, CGFloat radius){
        weakSelf.layer.borderColor = borderColor.CGColor;
        weakSelf.layer.borderWidth = borderWidth;
        weakSelf.layer.cornerRadius= radius;
        weakSelf.layer.masksToBounds = YES;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGFloat))js_radius{
    
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGFloat radius){
        weakSelf.layer.cornerRadius = radius;
        weakSelf.layer.masksToBounds = YES;
        return weakSelf;
    };
}

- (UIView * _Nonnull (^)(BOOL))js_userEnabled{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(BOOL enabled){
        weakSelf.userInteractionEnabled = enabled;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(BOOL))js_hidden{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(BOOL hidden){
        weakSelf.hidden = hidden;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGFloat))js_alpha{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGFloat alpha){
        weakSelf.alpha = alpha;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(UIViewContentMode))js_contentMode{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(UIViewContentMode mode){
        weakSelf.contentMode = mode;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGRect))js_frame{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGRect frame){
        weakSelf.frame = frame;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGPoint))js_center{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGPoint point){
        weakSelf.center = point;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGSize))js_size{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGSize size){
        weakSelf.size = size;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGFloat))js_left{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGFloat left){
        weakSelf.left = left;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGFloat))js_right{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGFloat right){
        weakSelf.right = right;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGFloat))js_top{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGFloat top){
        weakSelf.top = top;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGFloat))js_width{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGFloat width){
        weakSelf.width = width;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGFloat))js_height{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGFloat height){
        weakSelf.height = height;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(CGFloat))js_bottom{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(CGFloat bottom){
        weakSelf.bottom = bottom;
        return weakSelf;
    };
}
- (UIView * _Nonnull (^)(NSArray * _Nonnull))js_addSubViews{
    __weak typeof(self) weakSelf = self;
    return ^UIView*(NSArray *views){
        for (UIView *v in views) {
            [weakSelf addSubview:v];
        }
        return weakSelf;
    };
}

@end

@implementation UIButton (JSUITool)

- (UIButton * _Nonnull (^)(UIImage * _Nonnull, UIControlState))js_image{
    __weak typeof(self) weakSelf = self;
    return ^UIButton*(UIImage *image, UIControlState state){
        [weakSelf setImage:image forState:state];
        return weakSelf;
    };
}
- (UIButton * _Nonnull (^)(NSString * _Nonnull, UIControlState))js_title{
    __weak typeof(self) weakSelf = self;
    return ^UIButton*(NSString *title, UIControlState state){
        [weakSelf setTitle:title forState:state];
        return weakSelf;
    };
}
- (UIButton * _Nonnull (^)(UIColor * _Nonnull, UIControlState))js_textColor{
    __weak typeof(self) weakSelf = self;
    return ^UIButton*(UIColor *color, UIControlState state){
        [weakSelf setTitleColor:color forState:state];
        return weakSelf;
    };
}
- (UIButton * _Nonnull (^)(UIImage * _Nonnull))js_normalImage{
    __weak typeof(self) weakSelf = self;
    return ^UIButton*(UIImage *image){
        return weakSelf.js_image(image, UIControlStateNormal);
    };
}
- (UIButton * _Nonnull (^)(NSString * _Nonnull))js_normalTitle{
    __weak typeof(self) weakSelf = self;
    return ^UIButton*(NSString *title){
        return weakSelf.js_title(title, UIControlStateNormal);
    };
}
- (UIButton * _Nonnull (^)(UIColor * _Nonnull))js_textNormalColor{
    __weak typeof(self) weakSelf = self;
    return ^UIButton*(UIColor *color){
        return weakSelf.js_textColor(color, UIControlStateNormal);
    };
}
- (UIButton * _Nonnull (^)(UIFont * _Nonnull))js_textFont{
    __weak typeof(self) weakSelf = self;
    return ^UIButton*(UIFont *font){
        weakSelf.titleLabel.font = font;
        return weakSelf;
    };
}
- (UIButton * _Nonnull (^)(NSString * _Nonnull))js_debugText{
    __weak typeof(self) weakSelf = self;
    return ^UIButton*(NSString *text){
        if ([JSBusiness sharedInstance].isDebug) {
           return weakSelf.js_normalTitle(text);
        }
        return weakSelf;
    };
}
@end
